package com.webank.wedatasphere.qualitis.question.service.impl;

import com.webank.wedatasphere.qualitis.dao.UserDao;
import com.webank.wedatasphere.qualitis.entity.User;
import com.webank.wedatasphere.qualitis.question.constant.OrderStatus;
import com.webank.wedatasphere.qualitis.question.controller.vo.MyOrderDetailVo;
import com.webank.wedatasphere.qualitis.question.entity.Order;
import com.webank.wedatasphere.qualitis.question.repository.OrderRepository;
import com.webank.wedatasphere.qualitis.question.service.MyOrderService;
import com.webank.wedatasphere.qualitis.rule.dao.repository.RuleDetailInfoVoRepository;
import com.webank.wedatasphere.qualitis.rule.dao.repository.SqlTaskRepository;
import com.webank.wedatasphere.qualitis.rule.entity.RuleDetailInfoVo;
import com.webank.wedatasphere.qualitis.rule.entity.SqlTaskInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class MyOrderServiceImpl implements MyOrderService {

    @Resource
    private OrderRepository orderRepository;

    @Resource
    private SqlTaskRepository sqlTaskRepository;

    @Resource
    private RuleDetailInfoVoRepository ruleDetailInfoVoRepository;

    @Resource
    private UserDao userDao;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int transfer(List<Long> ids, Long handler) {
        List<Order> orderList = orderRepository.findAllById(ids);
        User user = userDao.findById(handler);
        for (Order order : orderList) {
            // 只有未处理和被打回的工单才能转交
            if (OrderStatus.getByCode(order.getStatus()) != OrderStatus.NOT_RESOLVE
                    && OrderStatus.getByCode(order.getStatus()) != OrderStatus.RETURN) {
                return 0;
            }
            order.setHandler(handler);
            order.setHandlerName(user.getChineseName());
            order.setUpdateTime(new Date());
        }
        List<Order> savedAll = orderRepository.saveAll(orderList);
        return savedAll.size();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int orders(List<Long> ids) {
        List<Order> orderList = orderRepository.findAllById(ids);
        Date now = new Date();
        for (Order order : orderList) {
            // 只有未处理和被打回的工单才能转交
            if (OrderStatus.getByCode(order.getStatus()) != OrderStatus.NOT_RESOLVE) {
                return 0;
            }
            order.setStatus(OrderStatus.RESOLVING.getCode());
            order.setUpdateTime(now);
            order.setOrderTime(now);
        }
        List<Order> savedAll = orderRepository.saveAll(orderList);
        return savedAll.size();
    }

    @Override
    public MyOrderDetailVo getDetailVo(Long id) {
        Order order = orderRepository.findById(id).orElse(new Order());
        MyOrderDetailVo myOrderDetailVo = new MyOrderDetailVo()
                .setId(id)
                .setStatus(order.getStatus())
                .setHandler(order.getHandler())
                .setHandlerName(order.getHandlerName())
                .setCause(order.getCause())
                .setPlan(order.getPlan())
                .setSuggestion(order.getSuggestion());
        if (order.getTaskId() == null) {
            return myOrderDetailVo;
        }

        SqlTaskInfo taskInfo = sqlTaskRepository.findById(order.getTaskId()).orElse(new SqlTaskInfo());

        myOrderDetailVo.setBeginTime(taskInfo.getBeginTime())
                .setEndTime(taskInfo.getEndTime())
                .setProjectName(taskInfo.getProjectName())
                .setResult(taskInfo.getResult());

        Long ruleNewId = taskInfo.getRuleNewId();
        if (ruleNewId == null) {
            return myOrderDetailVo;
        }

        RuleDetailInfoVo ruleDetail = ruleDetailInfoVoRepository.findById(ruleNewId).orElse(new RuleDetailInfoVo());

        myOrderDetailVo.setRuleName(ruleDetail.getRuleName()).setRuleType(ruleDetail.getRuleType())
                .setDataSourceEngine(ruleDetail.getDataSourceEngine()).setDataBaseName(ruleDetail.getDatabaseName())
                .setTableName(ruleDetail.getTableName()).setCompareType(ruleDetail.getCompareType())
                .setCheckThreshold(ruleDetail.getCheckThreshold()).setFieldName(ruleDetail.getFieldName());

        return myOrderDetailVo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean complete(Order order) {
        Order o = orderRepository.findById(order.getId()).orElse(new Order());
        if (o.getId() == null) {
            return false;
        }

        // 只能完成处理中和被打回的状态
        OrderStatus status = OrderStatus.getByCode(o.getStatus());
        if (status != OrderStatus.RESOLVING && status != OrderStatus.RETURN) {
            return false;
        }

        o.setCause(order.getCause())
                .setPlan(order.getPlan())
                .setSuggestion(order.getSuggestion())
                .setStatus(OrderStatus.RESOLVED.getCode())
                .setCompleteTime(new Date());

        Order save = orderRepository.save(o);

        return save.getId() != null;
    }


}
